package com.qbw.app.module.vehicle.holder;

import android.content.Context;
import android.view.ViewGroup;

import com.qbw.app.R;
import com.qbw.app.databinding.VehicleHolderSelectItemBinding;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.holder.CommonHolder;

/**
 * @author QBW
 * @date 2019/10/8
 */
public class VehicleSelectItemHolder extends CommonHolder<Vehicle, VehicleHolderSelectItemBinding> {

    public VehicleSelectItemHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.vehicle_holder_select_item, viewParent);
    }

    @Override
    public void bindData(int position, Vehicle data) {
        super.bindData(position, data);
        mBinding.setVehicle(data);
        mBinding.executePendingBindings();
        itemView.setOnClickListener(mClickListenerItem);
    }

    @Override
    protected void onClickItem() {
        if (getSelectedVehicleCount() <= 1 && mData.isSelected()) {
            return;
        }
        mData.setSelected(!mData.isSelected());
    }

    private int getSelectedVehicleCount() {
        if (mContext instanceof Listener) {
            return ((Listener) mContext).onGetNowSelectedVehicleCount();
        }
        return 0;
    }

    public interface Listener {
        int onGetNowSelectedVehicleCount();
    }
}
